﻿using BW.WMS.Application.Features.User.Dtos;
using BW.WMS.Application.Features.User.Interfaces;
using BW.WMS.Core.Result;
using BW.WMS.Domain.Entities.User;
using BW.WMS.Infrastructure.Data.Contexts;
using BW.WMS.Infrastructure.Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BW.WMS.Application.Features.User.Services
{
    public class UserServices : IUserServices
    {
        private readonly DataRepository<UserModel> _db; 

       
        public UserServices(DBContext dBContext)
        {
            _db = new DataRepository<UserModel>(dBContext);
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public ApiResult<UserDtos> Login(InputUserDtos inputUserDtos)
        {
            ApiResult<UserDtos> res = new ApiResult<UserDtos>();
            string Sql = @"  
		SELECT Id, Username, FullName, Phone, Email, Status, LastLoginTime, CreateTime, UpdateTime, IsDeleted 
                               FROM   User
                               WHERE Username = @Username AND PasswordHash = @Password AND IsDeleted = 0";


            var user = _db.GetList(Sql, new { Username = inputUserDtos.UserName, Password = inputUserDtos.Password }).FirstOrDefault();

            if(user == null)
            {
                res.Code = ApiEnums.Fail;
                res.Message = "用户名或密码错误";
            }
            else
            {
                res.Code = ApiEnums.Success;
                res.Message = $"{inputUserDtos.UserName}登录成功";
                res.Result=new UserDtos
                {
                    Id = user.Id,
                    Username = user.Username,
                    FullName = user.FullName,
                    Phone = user.Phone,
                    Email = user.Email,
                    Status = user.Status,
                    LastLoginTime = user.LastLoginTime,
                    CreateTime = user.CreateTime,
                    UpdateTime = user.UpdateTime,
                    IsDeleted = user.IsDeleted
                };
            }
            return res;
        }
    }
}
